package vincent.com.layouttest.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Path;
import android.util.AttributeSet;
import android.widget.ImageView;

/**
 * Created by zvin-chen on 16/1/28.
 */
public class ClipCircleImage extends ImageView {
    public ClipCircleImage(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ClipCircleImage(Context context) {
        this(context, null);
    }

    public ClipCircleImage(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        float cx = getMeasuredWidth()/2;
        float cy = getMeasuredHeight()/2;
        Path circlePath = new Path();
        circlePath.addCircle(cx, cy, cy, Path.Direction.CCW);

        canvas.save();
        canvas.clipPath(circlePath);
        super.onDraw(canvas);
        canvas.restore();
    }
}
